package com.itboye.mengwu.app.ui.aizhiwu;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.database.CursorJoiner.Result;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.adapter.MengTimesAdapter;
import com.itboye.mengwu.app.login.ui.LoginActivity;
import com.itboye.mengwu.data.model.LuntanTieZiBean;
import com.itboye.mengwu.data.model.ProductTypeBean;
import com.itboye.mengwu.data.model.TestModel;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.model.TieZiBaoHanReply.GetAList;
import com.itboye.mengwu.data.presenter.MyTieZiCenterPresenter;
import com.itboye.mengwu.data.presenter.TieZiPresenter;
import com.itboye.mengwu.widget.CarouselView;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

public class AizhiwuMengShiGuangItemFragment extends BaseActivity implements
		OnClickListener, Observer, IXListViewListener {

	// CarouselView topLunbo;
	private List<ImageView> bmps;
	private XListView listView;
	TieZiPresenter presenter;
	// private View headerView;
	MengTimesAdapter adapter;
	ImageView back;
	TextView txt_title;
	LuntanTieZiBean model = new LuntanTieZiBean();
	MyTieZiCenterPresenter presenter2;
	private TieZiBaoHanReply baoHanReply;
	String uid;
	String type = "load";
	private int pageNo = 1;
	ArrayList<GetAList> aLists = new ArrayList<>();
	RelativeLayout nodata;
	TextView nodata_tv;
	LinearLayout head_lin;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_mengshiguang;
	}

	@Override
	public void onMyCreate() {
		showProgressDialog("加载中，请稍后", true);
		txt_title.setText("时光机");
		listView.setXListViewListener(this);
		presenter2 = new MyTieZiCenterPresenter(this);
		listView.setPullLoadEnable(true);
		listView.setPullRefreshEnable(true);

		uid = SPUtils.get(this, null, SpPublicParams.UID, "") + "";
		presenter = new TieZiPresenter(this);
		model.setFid("9");
		model.setUid(uid);
		model.setPage_no(pageNo + "");
		presenter2.getInfo(model);
		// presenter.getInfo(model);
		// headerView =
		// LayoutInflater.from(AizhiwuMengShiGuangItemFragment.this)
		// .inflate(R.layout.item_mengshiguang_headerview, null);
		setData();
		head_lin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(
						AizhiwuMengShiGuangItemFragment.this,
						MengShiGuangLook.class);
				intent.putExtra("tid", "-1");
				startActivityForResult(intent, 101);
				// (intent);
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(
						AizhiwuMengShiGuangItemFragment.this,
						MengShiGuangLook.class);
				intent.putExtra("tid", aLists.get(position - 1).getTid());
				startActivity(intent);
			}
		});

	}

	/** 设置数据 */
	private void setData() {
		adapter = new MengTimesAdapter(AizhiwuMengShiGuangItemFragment.this,
				aLists, editOnclickListener);
		// listView.addHeaderView(headerView);
		listView.setAdapter(adapter);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		default:
			break;
		}
	}

	OnClickListener editOnclickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int position = (int) v.getTag();
			Intent intent = new Intent(AizhiwuMengShiGuangItemFragment.this,
					MengShiGuangLook.class);
			intent.putExtra("tid", aLists.get(position).getTid());
			intent.putExtra("edit", "edit");
			startActivityForResult(intent, 101);
		}
	};

	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		if (arg0 == 101 && arg1 == 102) {
			model.setFid("9");
			model.setUid(uid);
			model.setPage_no(pageNo + "");
			// presenter2.getInfo(model);
			presenter.getInfo(model);
		}
	};

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == TieZiPresenter.TieziFenYeSuccess) {
				stopLoad();
			} else if (entity.getEventType() == MyTieZiCenterPresenter.QueryMyTieZiSuccess) {
				baoHanReply = (TieZiBaoHanReply) entity.getData();
				if (type.equals("load")) {

				} else {
					aLists.clear();
				}
				aLists.addAll(baoHanReply.getList());
				if (aLists.size() == 0) {
					// listView.removeAllViews();
					nodata.setVisibility(View.VISIBLE);
					// listView.addHeaderView(headerView);
					listView.setVisibility(View.GONE);
					nodata_tv.setText("暂无数据哦，赶快去发布吧");
				} else {
					nodata.setVisibility(View.GONE);
					listView.setVisibility(View.VISIBLE);
				}

				adapter.notifyDataSetChanged();
				stopLoad();
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		showProgressDialog("刷新中。。。", true);
		pageNo = 1;
		type = "refresh";
		model.setFid("9");
		model.setUid(uid);
		model.setPage_no(pageNo + "");
		// presenter2.getInfo(model);
		presenter.getInfo(model);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		showProgressDialog("刷新中。。。", true);
		pageNo++;
		type = "load";
		model.setFid("9");
		model.setUid(uid);
		model.setPage_no(pageNo + "");
		// presenter2.getInfo(model);
		presenter.getInfo(model);
	}

	public void stopLoad() {
		listView.stopLoadMore();
		listView.stopRefresh();
	}
}
